﻿using System.Linq.Expressions;
using System.Web.Routing;
using TSharp.Core.Mvc.Extensions;

namespace System.Web.Mvc
{
    /// <summary>
    ///     link扩展
    ///     <para>2010/8/19</para>
    ///     <para>TANGJINGBO</para>
    ///     <para>tangjingbo</para>
    /// </summary>
    public static class UrlHelperExtensions
    {
        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="area">The area.</param>
        /// <returns></returns>
        public static String ActionT<TController>(this UrlHelper helper,
                                                  Expression<Action<TController>> action,
                                                  string area)
            where TController : Controller
        {
            var route = new RouteValueDictionary();
            route.Add("area", area);
            return ActionT(helper, action, route);
        }

        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static String ActionT<TController>(this UrlHelper helper,
                                                  Expression<Action<TController>> action,
                                                  object values)
            where TController : Controller
        {
            return ActionT(helper, action, new RouteValueDictionary(values));
        }

        /// <summary>
        ///     Actions the link.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static String ActionT<TController>(this UrlHelper helper,
                                                  Expression<Action<TController>> action,
                                                  RouteValueDictionary values)
            where TController : Controller
        {
            return GenerateUrlT(helper, action, values);
        }

        /// <summary>
        ///     Generates the URL.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="helper">The helper.</param>
        /// <param name="action">The action.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static String GenerateUrlT<TController>(this UrlHelper helper,
                                                       Expression<Action<TController>> action,
                                                       RouteValueDictionary values)
            where TController : Controller
        {
            RouteValueDictionary routingValues = ExpressionValuesHelper.GetRouteValuesFromExpression(action, values);

            RequestContext requestContext = null;
            if (helper != null)
                requestContext = helper.RequestContext;
            else
            {
                var ctx = new HttpContextWrapper(HttpContext.Current);
                requestContext = new RequestContext(ctx, new RouteData());
            }
            return UrlHelper.GenerateUrl(null, routingValues["Action"] as string, routingValues["Controller"] as string,
                                         routingValues, RouteTable.Routes, requestContext, true);
            // return null;
            //return helper.RouteLink( routingValues, new RouteValueDictionary(htmlAttributes));
        }
    }
}